package com.showde.shoppingmall.business.logic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.kim.net.NetEntity;
import com.showde.shoppingmall.business.NetCallbackImplBase.Notify;
import com.showde.shoppingmall.business.ProductCategoryBusiness;
import com.showde.shoppingmall.db.ProductCategoryDBCommand;
import com.showde.shoppingmall.domain.ProductCategory;

public final class ProductCategoryLogic {

	private static final String TAG = "ProductCategoryLogic";

	public static void updateProductCategory(final Context context,
			final UICallbackInterface callbackInterface) {

		ProductCategoryBusiness business = new ProductCategoryBusiness(
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						JSONObject jsonObject = netEntity.result;
						if (jsonObject != null) {
							try {
								String returnCode = jsonObject
										.getString("ReturnCode");
								if (!"0".equals(returnCode)) {
									callbackInterface.onError("parseError");
									return;
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
							Log.d(TAG, netEntity.result.toString());
							ProductCategoryDBCommand categoryDBCommand = new ProductCategoryDBCommand(
									context);
							List<ProductCategory> productCategories = null;
							productCategories = parseJson2Entity(jsonObject);
							if (productCategories.size() > 0) {
								categoryDBCommand.add(productCategories);
								CallbackData data = new CallbackData();
								data.data = productCategories;
								callbackInterface.onSuccess(data);
							} else {
								callbackInterface.onError("parseError");
							}
						}
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						callbackInterface.onError(errorMsg);
					}
				});
		business.doBusiness();
	}

	private static final List<ProductCategory> parseJson2Entity(
			JSONObject jsonObject) {
		List<ProductCategory> productCategories = new ArrayList<ProductCategory>();
		JSONArray jsonArray = null;
		try {
			jsonArray = jsonObject.getJSONArray("List");
			if (jsonArray != null && jsonArray.length() > 0) {
				JSONObject jsonPC = null;
				ProductCategory productCategory = null;
				for (int i = 0; i < jsonArray.length(); i++) {
					jsonPC = jsonArray.getJSONObject(i);
					productCategory = new ProductCategory();
					productCategory
							.setProductCategoryId(jsonPC.getString("id"));
					productCategory.setProductCategoryName(jsonPC
							.getString("name"));
					productCategory.setProductCategoryIconUrl(jsonPC
							.getString("logo"));
					productCategory.setProductCategoryDesc(jsonPC
							.getString("desc"));
					productCategory.setProductCategoryPid(jsonPC
							.getString("pid"));
					productCategories.add(productCategory);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return productCategories;

	}
}
